package com.weavernorth.util;

import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.wechat.util.Utils;

import java.util.HashMap;
import java.util.Map;

import static com.weavernorth.util.dateUtils.getWeekDays;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.util
 * @date: 2022/2/22 16:17
 * @description: 考勤工具类
 */
public class KaoQinUtil {

    /***
     * 获取人员排班班次-上班时间及下班时间
     * @param kqdate
     * @param userid
     * @param log
     * @return
     */
    public static Map<String, String> getShift(String kqdate, String userid, Logger log) {
        RecordSet rs = new RecordSet();
        RecordSet rs_search1 = new RecordSet();
        RecordSet rs_search2 = new RecordSet();
        String strSql = "";
        Map<String, String> result = new HashMap<>();
        Integer level = 0;
        String strSql1 = "select seclevel from HRMRESOURCE where id='" + userid + "'";
        log.info("查询人员的安全级别sql：" + strSql1);
        rs.execute(strSql1);
        while (rs.next()) {
            level = Integer.parseInt(Util.null2s(rs.getString("seclevel"), "0"));
            log.info("安全级别：" + level);
        }
        //人员考勤排班
        String strWeek = "";
        try {
            strWeek = getWeekDays(kqdate);
            strSql = "SELECT g.GROUPNAME,h.LASTNAME,h.WORKCODE,f.weekday,f.serialid,s.serial,w.ONOFFWORKTYPE,w.TIMES FROM KQ_GROUPMEMBER a \n" +
                    "INNER JOIN HRMRESOURCE h ON a.TYPEVALUE=h.departmentid\n" +
                    "INNER JOIN KQ_GROUP g ON a.GROUPID=g.ID\n" +
                    "INNER JOIN kq_fixedschedulce f ON a.GROUPID=f.GROUPID\n" +
                    "LEFT JOIN kq_ShiftManagement s ON f.serialid=s.ID\n" +
                    "INNER JOIN KQ_SHIFTONOFFWORKSECTIONS w ON w.serialid=s.ID\n" +
                    "WHERE  NVL(a.ISDELETE, 0)!=1 AND h.id='" + userid + "' and f.weekday='" + strWeek + "'\n" +
                    "GROUP BY g.GROUPNAME,h.LASTNAME,h.WORKCODE,f.weekday,f.serialid,s.serial,w.TIMES,w.ONOFFWORKTYPE";
            log.info("查询固定班次人员的上下班sql:" + strSql);
            rs_search1.execute(strSql);
            if (rs_search1.getCounts() > 0) {
                while (rs_search1.next()) {
                    String worktype = rs_search1.getString("ONOFFWORKTYPE");
                    String times = rs_search1.getString("TIMES");
                    if ("start".equals(worktype)) {
                        //上班时间
                        result.put("0", times);
                    } else {
                        //下班时间
                        result.put("1", times);
                    }
                }
            } else {
                //终端店铺人员考勤排班
                strSql = "SELECT g.GROUPNAME,a.KQDATE,h.lastname,h.WORKCODE,a.serialid,s.serial,w.ONOFFWORKTYPE,w.TIMES FROM kq_shiftschedule a \n" +
                        "INNER JOIN HRMRESOURCE h ON a.resourceid=h.id\n" +
                        "INNER JOIN KQ_GROUP g on a.groupid=g.id\n" +
                        "INNER JOIN kq_ShiftManagement s ON a.serialid=s.ID\n" +
                        "INNER JOIN KQ_SHIFTONOFFWORKSECTIONS w ON w.serialid=s.ID\n" +
                        "WHERE  NVL(a.ISDELETE, 0)!=1 and  h.id='" + userid + "' and a.kqdate='" + kqdate + "'\n" +
                        "ORDER BY KQDATE asc,w.ONOFFWORKTYPE desc";
                log.info("查询非固定班次人员的上下班sql:" + strSql);
                rs_search2.execute(strSql);
                while (rs_search2.next()) {
                    String worktype2 = rs_search2.getString("ONOFFWORKTYPE");
                    String times2 = rs_search2.getString("TIMES");
                    if ("start".equals(worktype2)) {
                        //上班时间
                        result.put("0", times2);
                    } else {
                        //下班时间
                        result.put("1", times2);
                    }
                }
            }
        } catch (Exception ex) {
            log.info("程序异常：" + ex.toString());
        } finally {
            return result;
        }
    }

    /***
     * 获取人员排班班次-休息开始时间及结束时间
     * @param kqdate
     * @param userid
     * @param log
     * @return
     */
    public static Map<String, String> getShift_Rest(String kqdate, String userid, Logger log) {
        RecordSet rs = new RecordSet();
        RecordSet rs_search1 = new RecordSet();
        RecordSet rs_search2 = new RecordSet();
        String strSql = "";
        Map<String, String> result = new HashMap<>();
        Integer level = 0;
        String strSql1 = "select seclevel from HRMRESOURCE where id='" + userid + "'";
        log.info("查询人员的安全级别sql：" + strSql1);
        rs.execute(strSql1);
        while (rs.next()) {
            level = Integer.parseInt(Util.null2s(rs.getString("seclevel"), "0"));
            log.info("安全级别：" + level);
        }
        //人员考勤排班
        String strWeek = "";
        try {
            strWeek = getWeekDays(kqdate);
            strSql = "SELECT g.GROUPNAME,h.LASTNAME,h.WORKCODE,f.weekday,f.serialid,s.serial,sr.resttype,sr.TIME FROM KQ_GROUPMEMBER a \n" +
                    "INNER JOIN HRMRESOURCE h ON a.TYPEVALUE=h.departmentid\n" +
                    "INNER JOIN KQ_GROUP g ON a.GROUPID=g.ID\n" +
                    "INNER JOIN kq_fixedschedulce f ON a.GROUPID=f.GROUPID\n" +
                    "LEFT JOIN kq_ShiftManagement s ON f.serialid=s.ID\n" +
                    "INNER JOIN kq_ShiftRestTimeSections sr ON sr.serialid=s.ID\n" +
                    "WHERE  NVL(a.ISDELETE, 0)!=1 AND h.id='" + userid + "' and f.weekday='" + strWeek + "'\n" +
                    "GROUP BY g.GROUPNAME,h.LASTNAME,h.WORKCODE,f.weekday,f.serialid,s.serial,sr.TIME,sr.resttype";
            log.info("查询固定班次人员的休息班次sql:" + strSql);
            rs_search1.execute(strSql);
            if (rs_search1.getCounts() > 0) {
                while (rs_search1.next()) {
                    String worktype = rs_search1.getString("resttype");
                    String times = rs_search1.getString("TIME");
                    if ("start".equals(worktype)) {
                        //休息开始时间
                        result.put("0", times);
                    } else {
                        //休息结束时间
                        result.put("1", times);
                    }
                }
            } else {
                //终端店铺人员考勤排班
                strSql = "SELECT g.GROUPNAME,a.KQDATE,h.lastname,h.WORKCODE,a.serialid,s.serial,sr.resttype,sr.TIME FROM kq_shiftschedule a \n" +
                        "INNER JOIN HRMRESOURCE h ON a.resourceid=h.id\n" +
                        "INNER JOIN KQ_GROUP g on a.groupid=g.id\n" +
                        "INNER JOIN kq_ShiftManagement s ON a.serialid=s.ID\n" +
                        "INNER JOIN kq_ShiftRestTimeSections sr ON sr.serialid=s.ID\n" +
                        "WHERE  NVL(a.ISDELETE, 0)!=1 and  h.id='" + userid + "' and a.kqdate='" + kqdate + "'\n" +
                        "ORDER BY KQDATE asc,sr.resttype desc";
                log.info("查询非固定班次人员的休息班次sql:" + strSql);
                rs_search2.execute(strSql);
                while (rs_search2.next()) {
                    String worktype2 = rs_search2.getString("resttype");
                    String times2 = rs_search2.getString("TIME");
                    if ("start".equals(worktype2)) {
                        //休息开始时间
                        result.put("0", times2);
                    } else {
                        //休息结束时间
                        result.put("1", times2);
                    }
                }
            }
        } catch (Exception ex) {
            log.info("程序异常：" + ex.toString());
        } finally {
            return result;
        }

    }
}
